package com.redfox.nestrecyclerviewtest;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by redfox on 2017/3/10.
 */

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MyViewHolder> {
    private final int TYPE_BANNER = 1;
    private final int TYPE_ITEM = 2;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        //public TextView textView;
        public MyViewHolder(View itemView) {
            super(itemView);
            //textView = (TextView) itemView.findViewById(R.id.title);
        }
    }



    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = null;
        if (viewType == TYPE_ITEM) {
            view = inflater.inflate(R.layout.grid_item_view, parent, false);
        }
        else if (viewType == TYPE_BANNER) {
            view = inflater.inflate(R.layout.banner_layout, parent, false);
        }
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        int viewType = getItemViewType(position);
        if (viewType == TYPE_ITEM) {
            ((TextView)holder.itemView.findViewById(R.id.title)).setText("This is item " + position);
        }
        else if (viewType == TYPE_BANNER) {
            RecyclerView bannerView = (RecyclerView) holder.itemView.findViewById(R.id.banner_grid);
            bannerView.setLayoutManager(new GridLayoutManager(holder.itemView.getContext(), 2));
            bannerView.setItemAnimator(new DefaultItemAnimator());
            bannerView.setAdapter(new BannerAdapter());
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return TYPE_BANNER;
        else
            return TYPE_ITEM;
    }

    @Override
    public int getItemCount() {
        return 50;
    }


}
